// JavaScript Document

//构建jxt类
Jxt = {
	version : '1.0',
	//判断是否为IE
	isIE : function(){return window.ActiveXObject ? true : false;}()
}

//为类添加属性
Jxt.apply = function(o, c, defaults){
    if(defaults){
        // no "this" reference for friendly out of scope calls
        Jxt.apply(o, defaults);
    }

    if(o && c && typeof c == 'object'){
        for(var p in c){
			
            o[p] = c[p];
        }
    }
    return o;
};

var idSeed = 0;

//加入属性
Jxt.apply(Jxt,{
		getDom : function(el){        //得到Dom对象
            if(!el || !document){
                return null;
            }
            return el.dom ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
        } ,
		getDoc : function(){      //得到 文档 对象
            return Jxt.get(document);
        },
		 getBody : function(){    //得到 Body 对象
            return Jxt.get(document.body || document.documentElement);
        },
		getPos : function(el){
			var target = Jxt.getDom(el);
			var pos = {x:target.offsetLeft, y:target.offsetTop};
			
			var target = target.offsetParent;
			while (target)
			{
				pos.x += target.offsetLeft;
				pos.y += target.offsetTop;
				
				target = target.offsetParent
			}
			
			return pos;
		},
		id : function(el, prefix){
            prefix = prefix || "ext-gen";

            el = Jxt.getDom(el);
            var id = prefix + (++idSeed);
            return el ? (el.id ? el.id : (el.id = id)) : id;
        },
		removeDom : function(el)
		{
			if(!Jxt.getDom(el)) return null;

			if(Jxt.isIE)
			{
				Jxt.getDom(el).removeNode(true);
			}
			else
			{
				Jxt.getDom(el).parentNode.removeChild(Jxt.getDom(el));
			}
		}
});

//继承方法
Jxt.Jxtend = function(destination,source)
{
	for (var prop in source) {
		if (prop == "prototype") {
			continue;
		}

		destination.prototype[prop] = source[prop];
	}

	// make a reference for parent and reference prototype
	destination.prototype.prototype = destination.prototype.parent = source.prototype;

	return destination;
};

//注册对象事件
Jxt.on = function(el, eventName, fn) { 
		 
	el = Jxt.getDom(el);

	if (!el || !fn) {
		return false; 
	} 

	// prevent unload errors with simple check 
	var wrappedFn = function(e) { 
		return typeof Jxt != 'undefined' ? fn(Jxt.getEvent(e)) : false; 
	}; 

   // var li = [el, eventName, fn, wrappedFn]; 

   // var index = listeners.length; 
   // listeners[index] = li;//将这4个东东保存进数组 
	Jxt.doAdd(el, eventName, wrappedFn, false); 

	return true; 

};

//加入事件
Jxt.doAdd = function (el, eventName, fn, capture) { 
	if (window.addEventListener) { 
		el.addEventListener(eventName, fn, (capture)); 
	} else if (window.attachEvent) {
		el.attachEvent("on" + eventName, fn);  
	}
};


//确保将事件绑定到element
Jxt.getEvent =  function(e) { 
	var ev = e || window.event; 

	if (!ev) { 
		var c = this.getEvent.caller; 
		while (c) { 
			ev = c.arguments[0]; 
			if (ev && Event == ev.constructor) { 
				break; 
			} 
			c = c.caller; 
		} 
	}
	
	return ev; 
}

//不同浏览器
window["undefined"]=window["undefined"];

//等待页面加载
Jxt.onReady = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;
            // kill the timer
            clearInterval(load_timer);
            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();
            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();
        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);
            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/
            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }
            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }
        load_events.push(func);
    }
})();

function get_obj(id)
{
	return document.getElementById(id);
}

function newXMLHttpRequest() 
{
	var http_request = false;
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) 
		 {
            http_request.overrideMimeType('text/xml');
         }
     }
	 else if (window.ActiveXObject) 
	 { // IE
         try 
		 {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } 
		 catch (e) 
		 {
            try 
			{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
			catch (e) {}
         }
    }

    if (!http_request) 
	{
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
	else
	{
		return http_request;
	}
}


function add_short_cut(image,url,describe)
{
	var req = newXMLHttpRequest();
	
	req.onreadystatechange = function() { short_cut_response(req); }
		
	req.open('GET', 'alt_shortcut.php?action=addShortcut&image=' + image + '&describe=' + encodeURIComponent(describe) + '&url=' + encodeURIComponent(url), true);
	
	req.send(null);
}

function short_cut_response(req)
{
	if (req.readyState == 4) 
	   {
		   if (req.status == 200) 
		   {
				top.shortcutFrame.location.reload(); 
		   } 
	   }	
}


function list_item_onclick(id) {
	if (!document.getElementById || !document.getElementsByTagName)
		return false;
	
	this.list_item = Jxt.getDom(id);
}


list_item_onclick.prototype.init = function() {
	
	if(!this.list_item) return false;
	
	var list_item = this.list_item.getElementsByTagName("div");
	for (var i = 0; i < list_item.length; i++)
	{
		list_item[i].onclick = function() {
			
			var checkbox = this.getElementsByTagName("input")[0];
			
			if(checkbox)
			{
				if(checkbox.checked == true)
				{
					checkbox.checked = false;
				}
				else
				{
					checkbox.checked = true;
				}
				
				if(this.className == '')
				{
					this.className = 'click';
				}
				else
				{
					this.className = '';
				}
			}
		}
		
		var checkbox = list_item[i].getElementsByTagName("input")[0];
		if ( checkbox ) {
			checkbox.onclick = function() {
				// opera does not recognize return false;
				this.checked = ! this.checked;
			}
		}
	}
}

list_item_onclick.prototype.click_all = function(obj) {
	if(!this.list_item) return false;

	var input_item = this.list_item.getElementsByTagName("input");

	for (var i = 0; i < input_item.length; i++)
	{
		if(obj.checked)
		{
			input_item[i].checked = true;
			if(Jxt.isIE)
			{
				input_item[i].parentElement.parentElement.className = 'click';
			}
			else
			{
				input_item[i].parentNode.parentNode.className = 'click';
			}
		}
		else
		{
			input_item[i].checked = false;
			if(Jxt.isIE)
			{
				input_item[i].parentElement.parentElement.className = '';
			}
			else
			{
				input_item[i].parentNode.parentNode.className = '';
			}
		}
		
	}
}

list_item_onclick.prototype.get_input_value = function() {
	if(!this.list_item) return false;
	
	var input_item = this.list_item.getElementsByTagName("input");
	var id = 0;
	var first = 0;
	for (var i = 0; i < input_item.length; i++)
	{
		if(input_item[i].checked)
		{
			if(first)
			{
				id = id +  ',' + input_item[i].value;
			}
			else
			{
				id = input_item[i].value;
				first = 1;
			}
		}	
	}
	
	return id;
}


function search_box()
{
	if(get_obj('search_box').style.display == 'none')
	{
		get_obj('search_btn').className = "hover";
		get_obj('search_box').style.display = '';
	}
	else
	{
		get_obj('search_btn').className = "";
		get_obj('search_box').style.display = 'none';
	}
}

function msg_box(msg,url)
{
	if(Jxt.getDom('bg_div')) Jxt.removeDom('bg_div');
	if(Jxt.getDom('msg_div')) Jxt.removeDom('msg_div');
	
	var btn = '';
	
	var window_height = ((window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight);
	
	window_height = document.body.scrollHeight > window_height ? document.body.scrollHeight : window_height;
	
	var window_width = document.body.scrollWidth;
	
	var obj = document.createElement('div');
	obj.setAttribute("id","bg_div");
	obj.style.position = "absolute";
	obj.style.zIndex = "10001";
	obj.style.backgroundColor = "#000";
	obj.style.left = "0px";
	obj.style.top = "0px";
	obj.style.filter = "alpha(opacity=5)";
	obj.style.opacity = 0.05;
	
	obj.style.width = window_width + "px"; 
	obj.style.height = window_height + "px";
   
	document.body.appendChild(obj);
	if(typeof url == 'string')
	{
		btn = '<a href="' + url + '"><img src="html/images/msg_ok.gif" border="0" align="absmiddle" /></a>&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	
	var s = GetPageScroll();
	var msg_box = document.createElement('div');
	msg_box.setAttribute("id","msg_div");
	msg_box.style.position = "absolute";
	msg_box.className = 'msg';
	msg_box.style.zIndex = "10002";
	msg_box.style.top = s.Y + 50 + "px";
	msg_box.style.left = ((window_width / 2) - 150) + "px";
	msg_box.style.backgroundColor = "#fff";
	msg_box.style.width = "300px"; 
	msg_box.style.height = "200px";
	msg_box.innerHTML = '<div class="top_msg" id="top_msg"><img src="html/images/information.gif" border="0" align="absmiddle" /> 系统提示信息</div><div class="content_msg">' + msg + '</div><div class="btn_msg">' + btn + ' <img src="html/images/msg_cancel.gif" border="0" align="absmiddle"  onclick="close_msg_box();" /></div>';
	
	document.body.appendChild(msg_box);
	
	Drag.init(Jxt.getDom('top_msg') , msg_box);
}

function close_msg_box()
{
	var is_ie = document.all ? true : false;  //判断是否是IE
	var msg_div = get_obj('msg_div');
	var bg_div = get_obj('bg_div');

	if(msg_div)
	{
		if(is_ie) msg_div.removeNode(true);
		else msg_div.parentNode.removeChild(msg_div);
	}
	
	if(bg_div)
	{
		if(is_ie) bg_div.removeNode(true);
		else bg_div.parentNode.removeChild(bg_div);
	}
	
	//var sel=$n("select");
	//for(var i=0;i<sel.length;i++){sel[i].style.visibility = "visible"}
}

function close_alert_msg_box()
{
	var msg_box = Jxt.getDom('msg');
	msg_box.style.display = 'none';
}

function select_box(title,intro,msg,url)
{
	close_msg_box();
	if(Jxt.getDom('bg_div')) Jxt.removeDom('bg_div');
	if(Jxt.getDom('select_div')) Jxt.removeDom('select_div');
	
	var btn = '';
	
	var window_height = ((window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight);
	
	window_height = document.body.scrollHeight > window_height ? document.body.scrollHeight : window_height;
	
	var window_width = document.body.scrollWidth;
	
	var obj = document.createElement('div');
	obj.setAttribute("id","bg_div");
	obj.style.position = "absolute";
	obj.style.zIndex = "10001";
	obj.style.backgroundColor = "#000";
	obj.style.left = "0px";
	obj.style.top = "0px";
	obj.style.filter = "alpha(opacity=5)";
	obj.style.opacity = 0.05;
	
	obj.style.width = window_width + "px"; 
	obj.style.height = window_height + "px";
   
	document.body.appendChild(obj);
	if(typeof url == 'string')
	{
		btn = '<a href="' + url + '"><img src="html/images/msg_ok.gif" border="0" align="absmiddle" /></a>&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	
	var s = GetPageScroll();
	//alert(s.Y);
	var select_box = document.createElement('div');
	select_box.setAttribute("id","select_div");
	select_box.style.position = "absolute";
	select_box.className = 'msg';
	select_box.style.zIndex = "10002";
	select_box.style.top = s.Y + 50 + "px";
	select_box.style.left = ((window_width / 2) - 400) + "px";
	select_box.style.backgroundColor = "#fff";
	select_box.style.width = "700px"; 
	select_box.innerHTML = '<div class="top_msg" id="top_msg"><img src="html/images/information.gif" border="0" align="absmiddle" />  ' + title + ' <span id="intro_msg"> ' + intro + ' </span></div><div id="content_msg">' + msg + '</div><div class="btn_msg">' + btn + ' <img src="html/images/msg_cancel.gif" border="0" align="absmiddle"  onclick="close_select_box();" /></div>';
	
	document.body.appendChild(select_box);
	
	Drag.init(Jxt.getDom('top_msg') , select_box);
}

function close_select_box()
{
	var is_ie = document.all ? true : false;  //判断是否是IE
	var select_div = get_obj('select_div');
	var bg_div = get_obj('bg_div');

	if(select_div)
	{
		if(is_ie) select_div.removeNode(true);
		else select_div.parentNode.removeChild(select_div);
	}
	
	if(bg_div)
	{
		if(is_ie) bg_div.removeNode(true);
		else bg_div.parentNode.removeChild(bg_div);
	}
	var sel=$n("select");
	for(var i=0;i<sel.length;i++){sel[i].style.visibility = "visible"}
}

var listItem;

Jxt.onReady(function(){

	listItem = new list_item_onclick('list_item');
	listItem.init();
	//控制input样式
	
	var elements = document.getElementsByTagName("input");  
	
	for(var i=0;i<elements.length;i++){   
		if(elements[i].type=="text" || elements[i].type=="textarea"  || elements[i].type=="password"){       
				elements[i].onfocus=function(){this.style.border = '1px solid #ff9900'; };  
				elements[i].onblur=function(){this.style.border = '1px solid #dbdbdb';};
		}   
	}   
	
	//控制消息显示框
	var msg_box = Jxt.getDom('msg');
	
	if(msg_box)
	{
		setTimeout('hidden_msg_box(1)',4000);
	}
});

function hidden_msg_box(speed)
{
	var msg_box = Jxt.getDom('msg');
	msg_box.style.marginTop = '-' + speed + 'px';
	
	if(speed < msg_box.offsetHeight)
	{
		speed = speed + 1;
		setTimeout('hidden_msg_box(' + speed + ')',5);
	}
	else
	{
		msg_box.style.display = 'none';
	}
}

function open_img(obj,src)
{
    var img_pos = getPos(obj);
	var img_obj = document.createElement("div") 
	img_obj.setAttribute("id","img_div"); 
	img_obj.style.position="absolute";
	
	var left = img_pos.x + 20;
	var top = img_pos.y;
	img_obj.style.left = left + "px"; 
	img_obj.style.top = top + "px"; 
	
	img_obj.style.background="#fff";
	img_obj.style.padding="4px";
	img_obj.style.border="1px solid #000";
	
	img_obj.innerHTML = "<img src=\"" + src + "\"  onMouseOut=\"close_img()\"  >";
	
	document.body.appendChild(img_obj);
}

function close_img()
{
	var img_obj = get_obj('img_div');

	if(img_obj)
	{
		if(Jxt.isIE)
		{
			img_obj.removeNode(true);
		}
		else
		{
			var tmp = img_obj.parentNode;
			tmp.removeChild(img_obj);
		}	
	}
}

function getPos(obj){
	var target = obj;
	var pos = {x:target.offsetLeft, y:target.offsetTop};
	
	var target = target.offsetParent;
	while (target)
	{
		pos.x += target.offsetLeft;
		pos.y += target.offsetTop;
		
		target = target.offsetParent
	}
	
	return pos;
}


// JavaScript Document
var Drag = {
	// 对这个element的引用，一次只能拖拽一个Element
	obj: null , 
	/**
	 * @param: elementHeader	used to drag..
	 * @param: element			used to follow..
	 */
	init: function(elementHeader, element) {
		// 将 start 绑定到 onmousedown 事件，按下鼠标触发 start
		elementHeader.onmousedown = Drag.start;
		// 将 element 存到 header 的 obj 里面，方便 header 拖拽的时候引用
		elementHeader.obj = element;
		// 初始化绝对的坐标，因为不是 position = absolute 所以不会起什么作用，但是防止后面 onDrag 的时候 parse 出错了
		if(isNaN(parseInt(element.style.left))) {
			element.style.left = "0px";
		}
		if(isNaN(parseInt(element.style.top))) {
			element.style.top = "0px";
		}
		// 挂上空 Function，初始化这几个成员，在 Drag.init 被调用后才帮定到实际的函数
		element.onDragStart = new Function();
		element.onDragEnd = new Function();
		element.onDrag = new Function();
	},
	// 开始拖拽的绑定，绑定到鼠标的移动的 event 上
	start: function(event) {
		var element = Drag.obj = this.obj;
		// 解决不同浏览器的 event 模型不同的问题
		event = Drag.fixE(event);
		// 看看是不是左键点击
		if(event.which != 1){
			// 除了左键都不起作用
			return true ;
		}
		// 参照这个函数的解释，挂上开始拖拽的钩子
		element.onDragStart();
		// 记录鼠标坐标
		element.lastMouseX = event.clientX;
		element.lastMouseY = event.clientY;
		// 绑定事件
		document.onmouseup = Drag.end;
		document.onmousemove = Drag.drag;
		return false ;
	}, 
	// Element正在被拖动的函数
	drag: function(event) {
		event = Drag.fixE(event);
		if(event.which == 0 ) {
		 	return Drag.end();
		}
		// 正在被拖动的Element
		var element = Drag.obj;
		// 鼠标坐标
		var _clientX = event.clientY;
		var _clientY = event.clientX;
		// 如果鼠标没动就什么都不作
		if(element.lastMouseX == _clientY && element.lastMouseY == _clientX) {
			return	false ;
		}
		// 刚才 Element 的坐标
		var _lastX = parseInt(element.style.top);
		var _lastY = parseInt(element.style.left);
		// 新的坐标
		var newX, newY;
		// 计算新的坐标：原先的坐标+鼠标移动的值差
		newX = _lastY + _clientY - element.lastMouseX;
		newY = _lastX + _clientX - element.lastMouseY;
		// 修改 element 的显示坐标
		element.style.left = newX + "px";
		element.style.top = newY + "px";
		// 记录 element 现在的坐标供下一次移动使用
		element.lastMouseX = _clientY;
		element.lastMouseY = _clientX;
		// 参照这个函数的解释，挂接上 Drag 时的钩子
		element.onDrag(newX, newY);
		return false;
	},
	// Element 正在被释放的函数，停止拖拽
	end: function(event) {
		event = Drag.fixE(event);
		// 解除事件绑定
		document.onmousemove = null;
		document.onmouseup = null;
		// 先记录下 onDragEnd 的钩子，好移除 obj
		var _onDragEndFuc = Drag.obj.onDragEnd();
		// 拖拽完毕，obj 清空
		Drag.obj = null ;
		return _onDragEndFuc;
	},
	// 解决不同浏览器的 event 模型不同的问题
	fixE: function(ig_) {
		if( typeof ig_ == "undefined" ) {
			ig_ = window.event;
		}
		if( typeof ig_.layerX == "undefined" ) {
			ig_.layerX = ig_.offsetX;
		}
		if( typeof ig_.layerY == "undefined" ) {
			ig_.layerY = ig_.offsetY;
		}
		if( typeof ig_.which == "undefined" ) {
			ig_.which = ig_.button;
		}
		return ig_;
	}
};


function GetPageScroll() {  
	var x, y;  
	
	if(window.pageYOffset) {    // all except IE    
		y = window.pageYOffset;   
		x = window.pageXOffset;  
	} 
	else if(document.documentElement     && document.documentElement.scrollTop) 
	{    // IE 6 Strict    
		y = document.documentElement.scrollTop;    
		x = document.documentElement.scrollLeft;  
	} else if(document.body) {    // all other IE    
		y = document.body.scrollTop;    
		x = document.body.scrollLeft;   
	}  
	
	return {X:x, Y:y};
}


function GetPageSize() {  
	var scrW, scrH;  
	if(window.innerHeight && window.scrollMaxY) {    // Mozilla    
		scrW = window.innerWidth + window.scrollMaxX;    
		scrH = window.innerHeight + window.scrollMaxY;  
	} else if(document.body.scrollHeight > document.body.offsetHeight){    // all but IE Mac    
		scrW = document.body.scrollWidth;    
		scrH = document.body.scrollHeight;  
	} else if(document.body) { // IE Mac    
		scrW = document.body.offsetWidth;    
		scrH = document.body.offsetHeight;  
	}  
	
	var winW, winH;  
	if(window.innerHeight) { // all except IE    
		winW = window.innerWidth;    
		winH = window.innerHeight;  
	} else if (document.documentElement     && document.documentElement.clientHeight) {    // IE 6 Strict Mode    
		winW = document.documentElement.clientWidth;     
		winH = document.documentElement.clientHeight;  
	} else if (document.body) { // other    
		winW = document.body.clientWidth;    
		winH = document.body.clientHeight;  
	}  // for small pages with total size less then the viewport  
	
	var pageW = (scrW<winW) ? winW : scrW;  
	var pageH = (scrH<winH) ? winH : scrH;  
	return {PageW:pageW, PageH:pageH, WinW:winW, WinH:winH};
}




window.$n=function(n){return document.getElementsByTagName?document.getElementsByTagName(n):new Array()}